const result = {
  /**
   * successSubscribe:订阅成功;
   * appletAuthFail:小程序授权失败;
   * appletAuthSuccess:小程序授权成功;
   * systemAuthFail:手机系统授权失败;
   * insufficientVersion:版本不足;
   * error:系统异常;
   * otherCode:其他状态码;
   * cancleSubscribe:取消订阅;
   * checkSubscribe:检查订阅;
   */
  status: "successSubscribe",
  message: "",
  originData: {},
  errMsg: "",
};
Component({
  data: {},
  properties: {
    title: String,
    styles: String,
    calendarOptions: {
      /**日历事件标题 */
      title: String,
      /**开始时间的 unix 时间戳（1970年1月1日开始所经过的时间），需要距离添加当日180天内 */
      startTime: Number,
      /**结束时间的 unix 时间戳（1970年1月1日开始所经过的时间），距离开始时间30天内 */
      endTime: Number,
      /**事件说明 */
      desc: String,
      /**事件位置 */
      location: String,
      /**是否设置提醒 */
      alarm: Boolean,
      /**提醒提前量（单位：分钟） */
      alarmOffset: Number,
    },
    /**拒绝后是否继续授权 */
    continueAuthorize: false,
  },
  lifetimes: {
    attached: async function () {
      try {
        const res = tt.canIUse("button.open-type.addCalendarEvent");
        if (!res) {
          result.status = "insufficientVersion";
          result.message = "版本不足";
          this.send();
          return;
        }
      } catch (error) {
        result.status = "error";
        result.message = "系统异常";
        result.errMsg = error;
        this.send();
      }
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  methods: {
    send() {
      this.triggerEvent("addcalendarevent", {
        ...result,
      });
    },
    async onAddCalendarEvent(e) {
      try {
        if (e.detail.errNo === 10200) {
          //未开启小程序权限
          if (this.data.continueAuthorize) {
            if (e.detail.errMsg.indexOf("fail system auth deny") > -1) {
              //是否为系统权限缺失
              result.status = "systemAuthFail";
              result.message = "手机系统授权失败";
            } else {
              const settingRes = await this.openSettingAsync("scope.calendar");
              if (settingRes) {
                result.status = "appletAuthSuccess";
                result.message = "小程序授权成功";
              } else {
                result.status = "appletAuthFail";
                result.message = "小程序授权失败";
              }
            }
          } else {
            result.status = "appletAuthFail";
            result.message = "小程序授权失败";
          }
        } else if (e.detail.errNo === 21102) {
          //取消日历订阅弹窗
          result.status = "cancleSubscribe";
          result.message = "取消订阅";
        } else if (e.detail.errNo === 0) {
          result.status = "successSubscribe";
          result.message = "订阅成功";
        } else {
          result.status = "otherCode";
          result.message = "其他状态码";
        }
        result.originData = e.detail;
        this.send();
      } catch (error) {
        result.status = "error";
        result.message = "系统异常";
        result.errMsg = error;
        this.send();
      }
    },
    /**开启前往权限内容 */
    openSettingAsync(value) {
      return new Promise((resolve, reject) => {
        tt.openSetting({
          success: res => {
            if (res.authSetting[value]) {
              resolve(true);
            } else {
              resolve(false);
            }
          },
          fail: res => {
            reject(res);
          },
        });
      });
    },
  },
});
